﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MLOCore.Utility
{
    internal class MP3ID3V1Tag : ID3Reader
    {
        
        #region "Genre Strings"
        private static string[] genres = {"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", 
    "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", 
    "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", 
    "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", 
    "Alt. Rock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", 
    "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta Rap", 
    "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychedelic", "Rave", "Showtunes", 
    "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", 
    "Folk", "Folk/Rock", "National Folk", "Swing", "Fast-Fusion", "Bebop", "Latin", "Revival", "Celtic", "Bluegrass", 
    "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", 
    "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", 
    "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", 
    "Duet", "Punk Rock", "Drum Solo", "A Cappella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass", "Club-House", "Hardcore", 
    "Terror", "Indie", "BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal", "Black Metal", "Crossover", 
    "Contemporary Christian", "Christian Rock", "Merengue", "Salsa", "Thrash Metal"};
        #endregion

        private static ID3Information _ReadFile(string fn)
        {

            FileStream stream;
            BinaryReader br;

            string title;
            string artist;
            string album;
            string genre;
            string comment;
            int year;
            int track;

            char[] ch;
            byte b;
            string s;

            try
            {
                stream = new FileStream(fn, FileMode.Open);
                br = new BinaryReader(stream);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not open " + fn, ex);
            }

            try
            {
                stream.Seek(-128, SeekOrigin.End);

                ch = br.ReadChars(3);
                if (!(new string(ch) == "TAG"))
                {
                    throw new Exception("No Valid ID3V1.1 tag information");
                }

                ch = br.ReadChars(30);
                title = new string(ch);
                ch = br.ReadChars(30);
                artist = new string(ch);
                ch = br.ReadChars(30);
                album = new string(ch);

                ch = br.ReadChars(4);
                s = new string(ch);
                int i;
                if (int.TryParse(s, out i))
                {
                    year = i;
                }
                else
                {
                    year = 0;
                }

                ch = br.ReadChars(28);
                comment = new string(ch);
                b = br.ReadByte();
                b = br.ReadByte();
                track = b;
                b = br.ReadByte();
                genre = genres[b];

                return new ID3Information(artist, album, title, genre, comment, year, track);

            }
            catch (Exception ex)
            {
                throw new Exception("ID3 V1.1 tag information not valid in " + fn, ex);
            }
            finally
            {
                br.Close();
                stream.Close();
            }

            //return ID3Information.Empty;
        }

        internal override bool CanProcessFile(string fileName)
        {
            if (System.IO.Path.GetExtension(fileName).ToLower() == ".mp3")
                return true;
            else
                return false;
        }

        internal override ID3Information ProcessFile(string fileName)
        {
            return _ReadFile(fileName);
        }
    }
}
